package cn.yr.controller.portal;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.yr.common.util.ip.IpUtils;
import cn.yr.domain.entity.AccessLog;
import cn.yr.service.portal.AccessLogService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RequestMapping("accessLog")
@RestController
public class AccessLogController {

	@Resource
	private AccessLogService accessLogService;

	@PostMapping("add")
	public void add(HttpServletRequest request){
		String uaStr = request.getHeader("User-Agent");
		AccessLog accessLog = new AccessLog();
		UserAgent ua = UserAgentUtil.parse(uaStr);
		accessLog.setBrowser(ua.getBrowser().toString());
		accessLog.setCreateTime(new Date());
		accessLog.setMobile(ua.isMobile()?1:0);
		accessLog.setOs(ua.getOs().toString());
		accessLog.setIp(IpUtils.getIpAddr(request));
		accessLogService.save(accessLog);
	}
}
